home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / Perl / lib / perl5 / 5.00502 / m68k-amigaos / CORE / handy.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-01  |  10.5 KB  |  344 lines

  1. /*    handy.h
  2.  *
  3.  *    Copyright (c) 1991-1997, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  */
  9.  
  10. #if !defined(__STDC__)
  11. #ifdef NULL
  12. #undef NULL
  13. #endif
  14. #ifndef I286
  15. #  define NULL 0
  16. #else
  17. #  define NULL 0L
  18. #endif
  19. #endif
  20.  
  21. #define Null(type) ((type)NULL)
  22. #define Nullch Null(char*)
  23. #define Nullfp Null(PerlIO*)
  24. #define Nullsv Null(SV*)
  25.  
  26. #ifdef TRUE
  27. #undef TRUE
  28. #endif
  29. #ifdef FALSE
  30. #undef FALSE
  31. #endif
  32. #define TRUE (1)
  33. #define FALSE (0)
  34.  
  35.  
  36. /* XXX Configure ought to have a test for a boolean type, if I can
  37.    just figure out all the headers such a test needs.
  38.    Andy Dougherty    August 1996
  39. */
  40. /* bool is built-in for g++-2.6.3, which might be used for an extension.
  41.    If the extension includes <_G_config.h> before this file then
  42.    _G_HAVE_BOOL will be properly set.  If, however, the extension includes
  43.    this file first, then you will have to manually set -DHAS_BOOL in 
  44.    your command line to avoid a conflict.
  45. */
  46. #ifdef _G_HAVE_BOOL
  47. # if _G_HAVE_BOOL
  48. #  ifndef HAS_BOOL
  49. #   define HAS_BOOL 1
  50. #  endif
  51. # endif
  52. #endif
  53.  
  54. /* The NeXT dynamic loader headers will not build with the bool macro
  55.    So declare them now to clear confusion.
  56. */
  57. #ifdef NeXT
  58. # undef FALSE
  59. # undef TRUE
  60.   typedef enum bool { FALSE = 0, TRUE = 1 } bool;
  61. # define ENUM_BOOL 1
  62. # ifndef HAS_BOOL
  63. #  define HAS_BOOL 1
  64. # endif /* !HAS_BOOL */
  65. #endif /* NeXT */
  66.  
  67. #ifdef __BEOS__
  68. #define HAS_BOOL 1    /* Is a typedef in system headers */
  69. #include <SupportDefs.h>
  70. #endif
  71.  
  72. #ifndef HAS_BOOL
  73. # if defined(UTS) || defined(VMS)
  74. #  define bool int
  75. # else
  76. #  define bool char
  77. # endif
  78. #endif
  79.  
  80. /* XXX A note on the perl source internal type system.  The
  81.    original intent was that I32 be *exactly* 32 bits.
  82.  
  83.    Currently, we only guarantee that I32 is *at least* 32 bits.
  84.    Specifically, if int is 64 bits, then so is I32.  (This is the case
  85.    for the Cray.)  This has the advantage of meshing nicely with
  86.    standard library calls (where we pass an I32 and the library is
  87.    expecting an int), but the disadvantage that an I32 is not 32 bits.
  88.    Andy Dougherty    August 1996
  89.  
  90.    There is no guarantee that there is *any* integral type with
  91.    exactly 32 bits.  It is perfectly legal for a system to have
  92.    sizeof(short) == sizeof(int) == sizeof(long) == 8.
  93.  
  94.    Similarly, there is no guarantee that I16 and U16 have exactly 16
  95.    bits.
  96.  
  97.    For dealing with issues that may arise from various 32/64-bit 
  98.    systems, we will ask Configure to check out 
  99.        SHORTSIZE == sizeof(short)
  100.        INTSIZE == sizeof(int)
  101.        LONGSIZE == sizeof(long)
  102.     LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
  103.        PTRSIZE == sizeof(void *)
  104.     DOUBLESIZE == sizeof(double)
  105.     LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
  106.     Most of these are currently unused, but they are mentioned here so
  107.     metaconfig will include the appropriate tests in Configure and
  108.     we can then start to consider how best to deal with long long
  109.     variables.
  110.    Andy Dougherty    April 1998
  111. */
  112.  
  113. typedef char        I8;
  114. typedef unsigned char    U8;
  115. /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
  116.    Please search CHAR_MAX in perl.h for further details. */
  117. #define U8_MAX PERL_UCHAR_MAX
  118. #define U8_MIN PERL_UCHAR_MIN
  119.  
  120. typedef short        I16;
  121. typedef unsigned short    U16;
  122. #define I16_MAX PERL_SHORT_MAX
  123. #define I16_MIN PERL_SHORT_MIN
  124. #define U16_MAX PERL_USHORT_MAX
  125. #define U16_MIN PERL_USHORT_MIN
  126.  
  127. #if LONGSIZE > 4
  128.   typedef int        I32;
  129.   typedef unsigned int    U32;
  130. # define I32_MAX PERL_INT_MAX
  131. # define I32_MIN PERL_INT_MIN
  132. # define U32_MAX PERL_UINT_MAX
  133. # define U32_MIN PERL_UINT_MIN
  134. #else
  135.   typedef long        I32;
  136.   typedef unsigned long    U32;
  137. # define I32_MAX PERL_LONG_MAX
  138. # define I32_MIN PERL_LONG_MIN
  139. # define U32_MAX PERL_ULONG_MAX
  140. # define U32_MIN PERL_ULONG_MIN
  141. #endif
  142.  
  143. #define BIT_DIGITS(N)   (((N)*146)/485 + 1)  /* log2(10) =~ 146/485 */
  144. #define TYPE_DIGITS(T)  BIT_DIGITS(sizeof(T) * 8)
  145. #define TYPE_CHARS(T)   (TYPE_DIGITS(T) + 2) /* sign, NUL */
  146.  
  147. #define Ctl(ch) ((ch) & 037)
  148.  
  149. #define strNE(s1,s2) (strcmp(s1,s2))
  150. #define strEQ(s1,s2) (!strcmp(s1,s2))
  151. #define strLT(s1,s2) (strcmp(s1,s2) < 0)
  152. #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
  153. #define strGT(s1,s2) (strcmp(s1,s2) > 0)
  154. #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
  155. #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
  156. #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
  157.  
  158. #ifdef HAS_MEMCMP
  159. #  define memNE(s1,s2,l) (memcmp(s1,s2,l))
  160. #  define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
  161. #else
  162. #  define memNE(s1,s2,l) (bcmp(s1,s2,l))
  163. #  define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
  164. #endif
  165.  
  166. /*
  167.  * Character classes.
  168.  *
  169.  * Unfortunately, the introduction of locales means that we
  170.  * can't trust isupper(), etc. to tell the truth.  And when
  171.  * it comes to /\w+/ with tainting enabled, we *must* be able
  172.  * to trust our character classes.
  173.  *
  174.  * Therefore, the default tests in the text of Perl will be
  175.  * independent of locale.  Any code that wants to depend on
  176.  * the current locale will use the tests that begin with "lc".
  177.  */
  178.  
  179. #ifdef HAS_SETLOCALE  /* XXX Is there a better test for this? */
  180. #  ifndef CTYPE256
  181. #    define CTYPE256
  182. #  endif
  183. #endif
  184.  
  185. #define isALNUM(c)    (isALPHA(c) || isDIGIT(c) || (c) == '_')
  186. #define isIDFIRST(c)    (isALPHA(c) || (c) == '_')
  187. #define isALPHA(c)    (isUPPER(c) || isLOWER(c))
  188. #define isSPACE(c) \
  189.     ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
  190. #define isDIGIT(c)    ((c) >= '0' && (c) <= '9')
  191. #ifdef EBCDIC
  192.     /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
  193. #   define isUPPER(c)    isupper(c)
  194. #   define isLOWER(c)    islower(c)
  195. #   define isPRINT(c)    isprint(c)
  196. #   define toUPPER(c)    toupper(c)
  197. #   define toLOWER(c)    tolower(c)
  198. #else
  199. #   define isUPPER(c)    ((c) >= 'A' && (c) <= 'Z')
  200. #   define isLOWER(c)    ((c) >= 'a' && (c) <= 'z')
  201. #   define isPRINT(c)    (((c) > 32 && (c) < 127) || isSPACE(c))
  202. #   define toUPPER(c)    (isLOWER(c) ? (c) - ('a' - 'A') : (c))
  203. #   define toLOWER(c)    (isUPPER(c) ? (c) + ('a' - 'A') : (c))
  204. #endif
  205.  
  206. #ifdef USE_NEXT_CTYPE
  207.  
  208. #  define isALNUM_LC(c) \
  209.     (NXIsAlpha((unsigned int)(c)) || NXIsDigit((unsigned int)(c)) || \
  210.      (char)(c) == '_')
  211. #  define isIDFIRST_LC(c) \
  212.     (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
  213. #  define isALPHA_LC(c)        NXIsAlpha((unsigned int)(c))
  214. #  define isSPACE_LC(c)        NXIsSpace((unsigned int)(c))
  215. #  define isDIGIT_LC(c)        NXIsDigit((unsigned int)(c))
  216. #  define isUPPER_LC(c)        NXIsUpper((unsigned int)(c))
  217. #  define isLOWER_LC(c)        NXIsLower((unsigned int)(c))
  218. #  define isPRINT_LC(c)        NXIsPrint((unsigned int)(c))
  219. #  define toUPPER_LC(c)        NXToUpper((unsigned int)(c))
  220. #  define toLOWER_LC(c)        NXToLower((unsigned int)(c))
  221.  
  222. #else /* !USE_NEXT_CTYPE */
  223. #  if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
  224.  
  225. #    define isALNUM_LC(c) \
  226.     (isalpha((unsigned char)(c)) || \
  227.      isdigit((unsigned char)(c)) || (char)(c) == '_')
  228. #    define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
  229. #    define isALPHA_LC(c)    isalpha((unsigned char)(c))
  230. #    define isSPACE_LC(c)    isspace((unsigned char)(c))
  231. #    define isDIGIT_LC(c)    isdigit((unsigned char)(c))
  232. #    define isUPPER_LC(c)    isupper((unsigned char)(c))
  233. #    define isLOWER_LC(c)    islower((unsigned char)(c))
  234. #    define isPRINT_LC(c)    isprint((unsigned char)(c))
  235. #    define toUPPER_LC(c)    toupper((unsigned char)(c))
  236. #    define toLOWER_LC(c)    tolower((unsigned char)(c))
  237.  
  238. #  else
  239.  
  240. #    define isALNUM_LC(c) \
  241.     (isascii(c) && (isalpha(c) || isdigit(c) || (c) == '_'))
  242. #    define isIDFIRST_LC(c)    (isascii(c) && (isalpha(c) || (c) == '_'))
  243. #    define isALPHA_LC(c)    (isascii(c) && isalpha(c))
  244. #    define isSPACE_LC(c)    (isascii(c) && isspace(c))
  245. #    define isDIGIT_LC(c)    (isascii(c) && isdigit(c))
  246. #    define isUPPER_LC(c)    (isascii(c) && isupper(c))
  247. #    define isLOWER_LC(c)    (isascii(c) && islower(c))
  248. #    define isPRINT_LC(c)    (isascii(c) && isprint(c))
  249. #    define toUPPER_LC(c)    toupper(c)
  250. #    define toLOWER_LC(c)    tolower(c)
  251.  
  252. #  endif
  253. #endif /* USE_NEXT_CTYPE */
  254.  
  255. #ifdef EBCDIC
  256. EXT int ebcdic_control _((int));
  257. #  define toCTRL(c)    ebcdic_control(c)
  258. #else
  259.   /* This conversion works both ways, strangely enough. */
  260. #  define toCTRL(c)    (toUPPER(c) ^ 64)
  261. #endif
  262.  
  263. /* Line numbers are unsigned, 16 bits. */
  264. typedef U16 line_t;
  265. #ifdef lint
  266. #define NOLINE ((line_t)0)
  267. #else
  268. #define NOLINE ((line_t) 65535)
  269. #endif
  270.  
  271.  
  272. /* This looks obsolete (IZ):
  273.  
  274.    XXX LEAKTEST doesn't really work in perl5.  There are direct calls to
  275.    safemalloc() in the source, so LEAKTEST won't pick them up.
  276.    Further, if you try LEAKTEST, you'll also end up calling
  277.    Safefree, which might call safexfree() on some things that weren't
  278.    malloced with safexmalloc.  The correct "fix" to this, if anyone
  279.    is interested, is to ensure that all calls go through the New and
  280.    Renew macros.
  281.     --Andy Dougherty        August 1996
  282. */
  283.  
  284. #ifndef lint
  285.  
  286. #define NEWSV(x,len)    newSV(len)
  287.  
  288. #ifndef LEAKTEST
  289.  
  290. #define New(x,v,n,t)    (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
  291. #define Newc(x,v,n,t,c)    (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
  292. #define Newz(x,v,n,t)    (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
  293.             memzero((char*)(v), (n)*sizeof(t))
  294. #define Renew(v,n,t) \
  295.       (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  296. #define Renewc(v,n,t,c) \
  297.       (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  298. #define Safefree(d)    safefree((Malloc_t)(d))
  299.  
  300. #else /* LEAKTEST */
  301.  
  302. #define New(x,v,n,t)    (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
  303. #define Newc(x,v,n,t,c)    (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
  304. #define Newz(x,v,n,t)    (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
  305.              memzero((char*)(v), (n)*sizeof(t))
  306. #define Renew(v,n,t) \
  307.       (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  308. #define Renewc(v,n,t,c) \
  309.       (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  310. #define Safefree(d)    safexfree((Malloc_t)(d))
  311.  
  312. #define MAXXCOUNT 1400
  313. #define MAXY_SIZE 80
  314. #define MAXYCOUNT 16            /* (MAXY_SIZE/4 + 1) */
  315. extern long xcount[MAXXCOUNT];
  316. extern long lastxcount[MAXXCOUNT];
  317. extern long xycount[MAXXCOUNT][MAXYCOUNT];
  318. extern long lastxycount[MAXXCOUNT][MAXYCOUNT];
  319.  
  320. #endif /* LEAKTEST */
  321.  
  322. #define Move(s,d,n,t)    (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
  323. #define Copy(s,d,n,t)    (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
  324. #define Zero(d,n,t)    (void)memzero((char*)(d), (n) * sizeof(t))
  325.  
  326. #else /* lint */
  327.  
  328. #define New(x,v,n,s)    (v = Null(s *))
  329. #define Newc(x,v,n,s,c)    (v = Null(s *))
  330. #define Newz(x,v,n,s)    (v = Null(s *))
  331. #define Renew(v,n,s)    (v = Null(s *))
  332. #define Move(s,d,n,t)
  333. #define Copy(s,d,n,t)
  334. #define Zero(d,n,t)
  335. #define Safefree(d)    (d) = (d)
  336.  
  337. #endif /* lint */
  338.  
  339. #ifdef USE_STRUCT_COPY
  340. #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
  341. #else
  342. #define StructCopy(s,d,t) Copy(s,d,1,t)
  343. #endif
  344.